package superman.app.gr100.biz.gr_expapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import iclass.Itools;
import superman.app.gr100.biz.gr_expapp.R;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * Created by Scor on 2015/12/2.
 */
public class Login extends AppCompatActivity {
    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button submit;
    private final int LOGIN_REQUEST = 1;
    private final int HTTP_RESQUEST = 1;
    private JSONObject jsonObject;
    private Itools itools;

    @Override
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        usernameEditText = (EditText) findViewById(R.id.username);
        passwordEditText = (EditText) findViewById(R.id.password);
        itools = new Itools();
        autoLogin();
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String urlStr = "http://192.168.1.211:8080/api";
                JSONObject user = new JSONObject();
                user.put("username",usernameEditText.getText());
                user.put("password",passwordEditText.getText());
                JSONObject data = new JSONObject();
                data.put("method","logistics.staff.login");
                data.put("appkey","111");
                data.put("timestamp","1111111");
                data.put("param",user);
                String method = "POST";
                itools.httprequest(urlStr,data);
            }
        });
    }

    protected Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message){
            switch (message.what){
                case LOGIN_REQUEST:
//                    jsonObject =  JSONObject.parseObject(String.valueOf(message.obj));
//                    SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
//                    if(jsonObject.getBoolean("result")){
//                        editor.putString("username",jsonObject.getString("username"));
//                        editor.putString("password",jsonObject.getString("password"));
//                        editor.putString("area",jsonObject.getString("area").toString());
//                        editor.putString("areaId", jsonObject.getString("areaId").toString());
//                        editor.commit();
                        Intent intent = new Intent(Login.this,MainActivity.class);
                        startActivity(intent);
//                    }else {
//                        editor.clear();
//                        Toast.makeText(Login.this,"账号密码错误",Toast.LENGTH_SHORT).show();
//                    }
                    break;
                case 2:
                    Log.d("scor","no more");
                    break;
                default:
                    break;
            }
        }
    };
    private void autoLogin(){
        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
        String username = preferences.getString("username","");
        String password = preferences.getString("password","");
        usernameEditText.setText(username);
        passwordEditText.setText(password);
        if(username == ""||password == ""){

        }else {
            int FLAG = HTTP_RESQUEST;
            JSONObject user = new JSONObject();
            user.put("username",username);
            user.put("password",password);
            JSONObject data = new JSONObject();
            data.put("method", "logistics.mission.list");
            data.put("appkey", "111");
            data.put("timestamp", "1111111");
            data.put("param", user.toString());
            String strUrl = "http://192.168.1.211:8080/api";
            String res = itools.httprequest(strUrl, data);
            Log.e("res",res);
            Message message =new Message();
            message.what = LOGIN_REQUEST;
            message.obj = res;
            handler.sendMessage(message);
        }
    }

}
